<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------

namespace app\services\pay;


use app\services\BaseServices;
use app\services\car\OrderPayServices;
use think\log;
use crmeb\services\SpreadsheetExcelService;
use app\Request;
use crmeb\services\app\MiniProgramService;
use crmeb\services\app\WechatService;
use app\api\controller\v3\WxPayController as v3WxPayController;
use app\api\controller\v3\SampleController;
use think\facade\Db;
use app\api\controller\v2\LogController;
class WxpayServices extends BaseServices
{
    /*
     * 微信支付
     * 对接约你游
     * 2024年7月18日13:40:26
     * */
    public function WeChatpay($data)
    {
        $log = new LogController();
        $log_id = $log->add_log($data);
        $appid = $data['appid'];//1是正式约你游环境2是测试约你游环境3是AI约你游
        if(!$appid||!$data['order_type']||!$data['pay_type']||!$data['orderon']||!$data['amount']||!$data['body']){
            return app('json')->fail('参数不完整。Incomplete parameters。');
        }
        if($data['pay_type']=='jsapi'){//******************************************小程序支付***************************************************
            if($data['order_type']==1){//直营支付
                MiniProgramService::get_vendorid($appid);
                $code=  MiniProgramService::jsPay($data['openid'], $data['orderon'], $data['amount'], "", $data['body'], "JSAPI");
            }else{//服务商jsapi支付
                if(!$data['vendorid'])app('json')->fail('服务商支付必传商户ID');
                $wx_applyment = Db::name('wx_applyment')->where('id',$data['vendorid'])->find();
                if(!$wx_applyment)app('json')->fail('服务商数据异常');
                $WxPay = new v3WxPayController();
                $code =  $WxPay->unifyPay($data['openid'], $data['orderon'], $data['amount'], $data['body'],$appid,$wx_applyment['mchid']);
            }
        }else{//******************************************APP支付***************************************************
            if($data['order_type']==1){//直营支付
                WechatService::get_vendorid($appid);
                $code = WechatService::appPay($data['openid'], $data['orderon'], $data['amount'], "", $data['body'], "APP",$appid);
            }else{//服务商app支付
                if(!$data['vendorid'])app('json')->fail('服务商支付必传商户ID');
                $wx_applyment = Db::name('wx_applyment')->where('id',$data['vendorid'])->find();
                if(!$wx_applyment)app('json')->fail('商户数据异常');
                $WxPay = new v3WxPayController();
                $code =  $WxPay->WxappPay($data['orderon'], $data['amount'], $data['body'],$appid,$wx_applyment['mchid']);
            }
            //******************************************app支付*********************************************************
        }
        if($code['package']){//package不为空说明请求成功
            $log->add_log($code,$log_id);
            return $code;
        }

    }
}